package com.cloudxingkong.dianyinke.NewTeacher.Ui;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.IsMobile;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.LoginJsonUtils;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.MD5Utils;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.TimerUtils;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Ui.BaseActivity;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.HashMap;

public class UpdatePsd extends BaseActivity {

	private EditText etGetYzm;
	private EditText etPassword;
	private EditText etPhoneNo;
	private Button btnGetYzm;
	private TimerUtils timerUtils;
	private String getPhoneNo;
	private String getYzm;
	private String getPassword;
	private SharedPreferences sharedPreferences;
	private ProgressDialog progressDialog;


	@Override
	protected void onCreateView (Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView (R.layout.activity_update_psd_backup);
		init ();
	}

	private void init () {
		sharedPreferences = getSharedPreferences ("data_dyk", 0);
		etGetYzm = (EditText) findViewById (R.id.up_et_yzm);
		etPassword = (EditText) findViewById (R.id.up_et_password);
		etPhoneNo = (EditText) findViewById (R.id.up_et_phoneno);
		btnGetYzm = (Button) findViewById (R.id.up_btn_getyzm);

		timerUtils = new TimerUtils (60 * 1000, 1000, btnGetYzm);

		progressDialog = new ProgressDialog (this);
		progressDialog.setIndeterminate (true);
		progressDialog.setCancelable (false);
		progressDialog.setTitle (null);
	}


	public void back (View view) {
		finish ();
	}


	/** 完成 */
	public void update (View view) {
		if (TextUtils.isEmpty (etGetYzm.getText ())) {
			Toast.makeText (this, "验证码为空", Toast.LENGTH_SHORT).show ();
			return;
		}
		if (TextUtils.isEmpty (etPassword.getText ())) {
			Toast.makeText (this, "密码为空", Toast.LENGTH_SHORT).show ();
			return;
		}
		progressDialog.setMessage ("正在修改...");
		progressDialog.show ();

		getPhoneNo = etPhoneNo.getText ().toString ();
		getYzm = etGetYzm.getText ().toString ();
		getPassword = etPassword.getText ().toString ();

		final String md5 = MD5Utils.getMD5 (getPassword);
		Log.e ("test", "修改密码 中的 MD5加密 " + md5);
		Log.e ("test", "用户手机号 " + getPhoneNo + " 用户验证码 " + getYzm + " 用户密码 " + md5);

		RequestParams params = new RequestParams (ThisUri.SERVER_YUMING + ThisUri.UPDATE_PWD);
		params.addBodyParameter ("phone", getPhoneNo);
		params.addBodyParameter ("password", md5);
		params.addBodyParameter ("smscode", getYzm);

		x.http ().post (params, new Callback.CommonCallback<String> () {
			@Override
			public void onSuccess (String result) {
				Message message = handler.obtainMessage ();
				message.what = 1;
				message.obj = result;
				handler.sendMessage (message);
			}

			@Override
			public void onError (Throwable ex, boolean isOnCallback) {
				progressDialog.dismiss ();
				Log.e ("test", "失败请求修改信息 " + ex.getMessage ());
			}

			@Override
			public void onCancelled (CancelledException cex) {

			}

			@Override
			public void onFinished () {

			}
		});

	}

	/** 获取验证码 */
	public void getYzm (View view) {
		if (TextUtils.isEmpty (etPhoneNo.getText ())) {
			Toast.makeText (this, "手机号码为空", Toast.LENGTH_SHORT).show ();
			return;
		} else {
			boolean mobileNO = IsMobile.isMobileNO (etPhoneNo.getText ().toString ());
			if (!mobileNO) {
				Toast.makeText (UpdatePsd.this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show ();
				return;
			}
			progressDialog.setMessage ("正在获取验证码...");
			progressDialog.show ();

			getPhoneNo = etPhoneNo.getText ().toString ();

			Log.e ("test", "手机号 " + getPhoneNo);
			RequestParams params = new RequestParams (ThisUri.SERVER_YUMING + ThisUri.REST_PWD_GET_YZM);
			params.addBodyParameter ("phone", getPhoneNo);
			params.addBodyParameter ("product", "电音课Android");
			x.http ().post (params, new Callback.CommonCallback<String> () {
				@Override
				public void onSuccess (String result) {
					HashMap<String, Object> fromLogin = LoginJsonUtils.getJsonFromLogin (result);
					Log.e ("test", "成功请求修改信息： " + result);
					String success = fromLogin.get ("success").toString ();
					if (success.equals ("true")) {
						Log.e ("test", "成功请求验证码： " + success);
						Toast.makeText (UpdatePsd.this, "成功请求验证码", Toast.LENGTH_SHORT).show ();
						progressDialog.dismiss ();
						timerUtils.start ();
					} else {
						Toast.makeText (UpdatePsd.this, "请求失败，请检查网络", Toast.LENGTH_SHORT).show ();
						progressDialog.dismiss ();
					}
				}

				@Override
				public void onError (Throwable ex, boolean isOnCallback) {
					Log.e ("test", "失败请求注册信息 " + ex.getMessage ());
					Toast.makeText (UpdatePsd.this, "获取失败", Toast.LENGTH_SHORT).show ();
					progressDialog.dismiss ();
				}

				@Override
				public void onCancelled (CancelledException cex) {

				}

				@Override
				public void onFinished () {

				}
			});


		}
	}


	private Handler handler = new Handler () {
		@Override
		public void handleMessage (Message msg) {
			super.handleMessage (msg);
			if (msg.what == 1) {
				String result = (String) msg.obj;

				HashMap<String, Object> fromLogin = LoginJsonUtils.getJsonFromLogin (result);
				Log.e ("test", "成功请求修改信息： " + result);
				String success = fromLogin.get ("success").toString ();

				if (success.equals ("true")) {
					Log.e ("test", "成功请求修改信息 " + result);

					Toast.makeText (UpdatePsd.this, "修改成功,请使用新密码登陆!", Toast.LENGTH_SHORT).show ();
					progressDialog.dismiss ();
					finish ();
				} else {
					progressDialog.dismiss ();
					Toast.makeText (UpdatePsd.this, "修改失败，请重试", Toast.LENGTH_SHORT).show ();
				}
			}
		}
	};
}
